<?php
/*
*
* @author rana
*
*/

// Strip out javascript, styles etc..
$GLOBALS['link_regexp'] = array ("/javascript\s*:/si","/^#/si","/return\s+/si");              

class linkchecker {

	var $scheme;  //- e.g. http 

	var $host; 

	var $port; 

	var $user; 

	var $pass; 

	var $path; 

	var $query; //- after the question mark ? 

	var $fragment; //- after the hashmark # 


	function linkchecker($url) {
		$info = parse_url($url);
		$this->scheme = @$info['scheme']; 
		$this->host = @$info['host'];
		$this->port = @$info['port'];
		$this->user = @$info['user'];
		$this->pass = @$info['pass'];
		$this->path = @$info['path'];
		$this->query = @$info['query'];
		$this->fragment = @$info['fragment'];
	}

	function get_links($link_data, $sitename = '', $read_links=false) {
		$mylinks = array();
		$temp_dup = array_keys($GLOBALS['URL_INFO']['LINKS']);

		foreach ($link_data as $link) {

			$attribs = $link['attributes'];
			$href = trim($attribs['href']);
			//$href = $this->remove_base($href);

			if (empty($href) ) continue;
			if (in_array($href, $temp_dup)) continue;

			if ($this->is_well_link($href)) {

				$title = trim(strip_tags($link['text']));
				$title = str_replace(array("\t","\n","\r\n"), array(' ','',''), $title);
				$title = trim(html_entity_decode($title));

				if(empty($title)) {
					$title = @$attribs['alt'];
				}
				if(empty($title)) {
					$title = @$attribs['title'];
				}
				if(empty($title)) {
					$title = $sitename;
				}
				$shoart_href = $this->remove_base($href);
				$full_href = $this->get_url_with_base($GLOBALS['URL_INFO']['BASE'], $href);
				
				if ($read_links) {
					$uread = new urlreader();
					$headers = $uread->get_headers_only($full_href,$shoart_href);
					$last_modified = @$headers['Last-Modified'];
					$status = parse_response_status($headers[0]);
					if (!is_good_status($status) ) continue;
				}

				$temp_dup[] = $shoart_href;
				$mylinks[$shoart_href] = array('title'=>$title,'link'=>$full_href,'last_modified'=>@$last_modified,'freq'=>'daily','status'=>@$status[0],'priority'=>'0.5');
			}

		}

		return $mylinks;
	}

	//
	function is_well_link($link) {

		foreach ($GLOBALS['link_regexp'] as $regex) {
			$found = preg_match($regex, $link);
			if ($found > 0)
				return false;
		}
		$is_inboud_link = $this->check_inbound($link);

		return $is_inboud_link;
	}
	
	//
	function check_inbound($link) {
		$flag = false;

		$info = parse_url($link);
		
		if ( empty($info['scheme']) || $info['scheme'] == 'http' || $info['scheme'] == 'https' )
			$flag = true;
		
		if ( $flag && ( empty($info['host']) || $info['host'] == $this->host) )
			$flag = true;
		else
			$flag = false;

		if ( empty($info['scheme']) && empty($info['host']) && empty($info['path']) && !empty($info['fragment']) )
			$flag = false;
		
		//if calling a javascript function
		if ( empty($info['scheme']) && empty($info['host']) && !empty($info['path']) ) {
			if (preg_match("/[a-zA-Z_]\s*\(.*\)/i",$info['path']) )
				$flag = false;
		}
		return $flag;
	}

	function get_base() {
		$site = '';

		if (empty($this->scheme)){
			$this->scheme = 'http';
		}
		$site = $this->scheme.'://';

		if (empty($this->host)){
			$paths = explode('/', $this->path);
			foreach($paths as $path) {
				$path = trim($path);
				if (!empty($path)) {
					$site .= $path;
					break;
				}
			}
		} else {
			$site .= $this->host;
	    }

		if (!empty($this->port) ){
			$site .= ':'.$this->port;
		}
		return $site;
	}

	function get_full_url($url) {
		$str1 = explode("://",$url);
		$site = '';

		if ( count($str1) == 2  ) {

			if ($str1[0] == 'http' || $str1[0] == 'https') {
				$scheme = $str1[0];
			}
			else {
				$scheme = 'http';
			}
			$host = $str1[1];
		} else {

			$scheme = 'http';
			$host = $str1[0];
		}
		
		$paths = explode('/', $host);
		foreach($paths as $path) {

			$path = trim($path);

			if (!empty($path)) {
				$site .= $path.'/';
			}
		}

		if (substr($site,-1) == '/' )
				$site = substr($site,0,-1);
		return $scheme . '://' . $site;
	}

	function get_url_with_base($base, $url) {
		$site = '';
		$info = parse_url($url);
		$binfo = parse_url($base);

		$site = $binfo['scheme'] .'://'.$binfo['host'];

		if (!empty($binfo['port']) ){
			$site .= ':'.$binfo['port'];
		}

		if (!empty($info['path']) ){
			if (substr($info['path'],0,1) != '/' )
				$site .= '/';
			$site .= $info['path'];
		}
		if (!empty($info['query']) ){
			$site .= '?'.$info['query'];
		}
		if (!empty($info['fragment']) ){
			$site .= '#'.$info['fragment'];
		}
		return $site;
	}

	//
	function modify_headers($page, $headers) {
		//$page = $this->remove_base($page);
		if (isset($GLOBALS['URL_INFO']['LINKS'][$page])) {
			$GLOBALS['URL_INFO']['LINKS'][$page]['last_modified'] = @$headers['Last-Modified'];
			$status = parse_response_status($headers[0]);
			$GLOBALS['URL_INFO']['LINKS'][$page]['status'] = $status[0];
			$GLOBALS['URL_INFO']['LINKS'][$page]['freq'] = 'daily';
			$GLOBALS['URL_INFO']['LINKS'][$page]['priority'] = '0.5';
		}
	}

	//
	function remove_base($page) {
		//$page = str_replace($GLOBALS['URL_INFO']['BASE'],"",$page);
		$page = trim($page);
		$info = parse_url($page);
		$site = '/';

		if (!empty($info['path']) && $info['path'] != '/' ){
			$site .= '/' . $info['path'];
		}
		if (!empty($info['query']) ){
			$site .= '?'.$info['query'];
		}
		if (!empty($info['fragment']) ){
			$site .= '#'.$info['fragment'];
		}
		$start = substr($site,0,1);
		while ($start == '/'){
			$site = substr($site,1);
			$start = substr($site,0,1);
		}
		return $site;
	}
}
?>